var app = getApp();
var SERVER_URL=app.globalData.SERVER_URL;
var dateTimePicker = require('./dateTimePicker.js');
function convertFormart(e){
    var year = 2010;
    var arrValue = e.detail.value;
    year +=arrValue[0];
    var month = arrValue[1]+1;
    var day =arrValue[2]+1;
    var h =arrValue[3];
    var m =arrValue[4];
    var strDate = year +"/"+month+"/"+day+" "+h+":"+m;
    var objDate = new Date(strDate);
    strDate =objDate.Format("YYYY-MM-dd hh:mm");
    return strDate;
}
Date.prototype.Format = function(formatStr)
{
    var str = formatStr;
    var Week = ['日','一','二','三','四','五','六'];

    str=str.replace(/yyyy|YYYY/,this.getFullYear());
    str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));

    str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + (this.getMonth()+1));
    str=str.replace(/M/g,this.getMonth());

    str=str.replace(/w|W/g,Week[this.getDay()]);

    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    str=str.replace(/d|D/g,this.getDate());

    str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
    str=str.replace(/h|H/g,this.getHours());
    str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
    str=str.replace(/m/g,this.getMinutes());

    str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
    str=str.replace(/s|S/g,this.getSeconds());

    return str;
};
Page({
    data: {
        date: '',
        time: '',
        dateTimeArray: null,
        dateTime: null,
        dateTimeArray1: null,
        dateTime1: null,
        startYear: 2010,
        endYear: 2020
    },
    onLoad(){
        // 获取完整的年月日 时分秒，以及默认显示的数组
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        // 精确到分的处理，将数组的秒去掉
        var lastArray = obj1.dateTimeArray.pop();
        var lastTime = obj1.dateTime.pop();
        var date=new Date();
        this.setData({
            dateTime: obj.dateTime,
            dateTimeArray: obj.dateTimeArray,
            dateTimeArray1: obj1.dateTimeArray,
            dateTime1: obj1.dateTime,
            time:date.Format("yyyy-MM-dd hh:mm"),
            time1:date.Format("yyyy-MM-dd hh:mm"),
        });
    },
    onShow:function(){
        this.setData({
            ApplyTrip:app.globalData.data.jdRemark||'150字以内',
            Participant:app.globalData.data.jdParticipant||'150字以内',
        });
    },
    slt_click:function(e){
        var index = e.currentTarget.dataset.index;
        index =parseInt(index);
        if(index==1){
            wx.navigateTo({
                url: './desc'
            });
        }
        else if(index==2){
            wx.navigateTo({
                url: './desc2'
            });
        }
    },
    changeDateTime:function(e){
        var strDate =convertFormart(e);
        this.setData({
            time:strDate
        });
    },
    changeDateTime1:function(e){
        var strDate =convertFormart(e);
        this.setData({
            time1:strDate
        });
    },
    form_submit:function(){
        var data = this.data;
        var Content = data.Content ||"";
        var Proposer = data.Proposer ||"";
        var AdmitPerson = data.AdmitPerson ||"";
        var ApplyTrip=app.globalData.data.jdRemark ||"";
        var Participant=app.globalData.data.jdParticipant ||"";

        if(Proposer==""){
            wx.showModal({
                title: '失败',
                content: '申请人不能为空',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }
        if(AdmitPerson==""){
            wx.showModal({
                title: '失败',
                content: '接待人员不能为空',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }
        if(ApplyTrip==""){
            wx.showModal({
                title: '失败',
                content: '接待事由不能为空',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }


        //submit
        var openId = wx.getStorageSync('openId');
        var url="/examine/reception";
        url = SERVER_URL+url;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                "OpenId": openId,
                "Type": 4,
                "Content":Content,
                "Proposer":Proposer,
                "AdmitPerson":AdmitPerson,
                "Participant":Participant,
                "ApplyTrip":ApplyTrip,
            },
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                console.log(result);
                if(result && result.code==0){
                    app.globalData.data.jdRemark="";
                    app.globalData.data.jdParticipant="";
                    wx.showModal({
                        title: '成功',
                        content: '已成功提交申请',
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }
                        }
                    });
                }
                else{
                    wx.showModal({
                        title: '失败',
                        content: result.msg,
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {

                            }
                        }
                    });
                }
            }
        })
    },
    text_change:function(e){
        var index = e.currentTarget.dataset.text;
        index = parseInt(index);
        var value = e.detail.value;
        switch (index){
            //备注
            case 1:
                this.setData({
                    Content:value
                });
                break;
            //申请人
            case 2:
                this.setData({
                    Proposer:value
                });
                break;
            //接待人
            case 3:
                this.setData({
                    AdmitPerson:value
                });
                break;
        }
    }
});